package lt.startup.duobes;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;

public class LocationView extends View {

	private Drawable pointer;
	
	public LocationView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	public LocationView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public LocationView(Context context) {
		super(context);
		init(context);
	}
	
	private void init(Context context){
		pointer = context.getResources().getDrawable(R.drawable.ash);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (Constants.map != null && Constants.location != null){
			final Projection proj = Constants.map.getProjection();
			final GeoPoint gPoint = new GeoPoint((int)(Constants.location.getLatitude()*1000000),(int)(Constants.location.getLongitude()*1000000));
			final Point point = proj.toPixels(gPoint, null);
			final int left = point.x-pointer.getIntrinsicWidth()/2;
			final int top = point.y-pointer.getIntrinsicHeight()/2;
			final int right = point.x+pointer.getIntrinsicWidth()/2;
			final int bottom = point.y+pointer.getIntrinsicHeight()/2;
			if (Constants.location.hasAccuracy()){
			final float rad = proj.metersToEquatorPixels(Constants.location.getAccuracy());
				Paint paint = new Paint();
				paint.setARGB(30, 100, 100, 200);
				paint.setStyle(Style.FILL);
				canvas.drawCircle(point.x, point.y, rad, paint);
				paint.setARGB(255, 100, 100, 200);
				paint.setStyle(Style.STROKE);
				canvas.drawCircle(point.x, point.y, rad, paint);
			}		
			pointer.setBounds(left, top, right, bottom);
			pointer.draw(canvas);			
		}	
		super.onDraw(canvas);
	}
}
